package com.hemaapp.yjnh.adapter;

import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.CunyouActivity;
import com.hemaapp.yjnh.bean.ZouzouDistrict;

/**村游地级市列表的适配器
 * @author GuiyouZhang
 * 2015年9月14日
 */
public class CunyouDistrictAdapter extends BaseAdapter {

	private ArrayList<ZouzouDistrict> districts;
	private CunyouActivity activity;
	
	public CunyouDistrictAdapter(CunyouActivity activity, ArrayList<ZouzouDistrict> districts) {
		super(activity);
		this.activity = activity;
		this.districts = districts;
	}

	@Override
	public int getCount() {
		int size = districts == null ? 0 : districts.size();
		return size == 0 ? 1 : districts.size();
	}
	
	@Override
    public boolean isEmpty() {
        int size = districts == null? 0:districts.size();
        return size == 0;
    }

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (isEmpty()) 
			return getEmptyView(parent);
		ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.listitem_zouzou_district, null);
            holder = new ViewHolder();
            findView(convertView, holder);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        }
		setData(position, convertView);
		return convertView;
	}

	private class ViewHolder {
		private LinearLayout ll_contain;
		private TextView tv_city;
//		private TextView tv_area;
	}
	
	private void findView(View convertView, ViewHolder holder) {
		holder.ll_contain = (LinearLayout)convertView.findViewById( R.id.ll_contain);
		holder.tv_city = (TextView)convertView.findViewById( R.id.tv_city );
//		holder.tv_area = (TextView)convertView.findViewById( R.id.tv_area );
	}
	
	private void setData(int position, View convertView) {
		ZouzouDistrict district = districts.get(position);
		ViewHolder holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
		holder.tv_city.setText(district.getcity_name());

		if (holder.ll_contain.getChildCount() != 0) {
			holder.ll_contain.removeAllViews();
		}
		String[] towns = district.gettowns();
		if (towns != null && towns.length > 0) {
			int length = towns.length/3+towns.length%3;//一共显示的行数
			String showtext = "";
			for (int i = 0; i < towns.length; i++) {
				showtext = showtext+towns[i];
				if (i%3 == 0) {
					View view = LayoutInflater.from(mContext).inflate(
							R.layout.layout_zouzou_country, null);
					LinearLayout ll_area = (LinearLayout) view.findViewById(R.id.ll_area);
					TextView textView = (TextView) view.findViewById(R.id.tv_area);
					textView.setText(showtext);
					holder.ll_contain.addView(ll_area);
					showtext = "";
				}
			}
		}
		convertView.setTag(district);
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ZouzouDistrict district2 = (ZouzouDistrict) v.getTag();
				activity.openDrawer(district2);
			}
		});
	}
}
